package com.c.ctgproject.adapter.guang

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.recyclerview.widget.RecyclerView
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseEmptyRecyclerViewAdapterV2
import com.c.ctgproject.entity.guang.*
import com.c.ctgproject.ui.collect.*
import com.c.ctgproject.utils.MyConstants
import com.c.ctgproject.utils.getGson
import com.c.ctgproject.utils.handlerNull
import com.c.ctgproject.utils.noDataHandler
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.textColorResource
import java.lang.StringBuilder

class PartHandlerAdapter(private val context: Context, private val mList: List<PartHandlerListEntity.DataBean.ListBean>, private val listener: PartHandlerAdapterListener) : BaseEmptyRecyclerViewAdapterV2(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_part_handler_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            holder.mUserNameTV.text = data.createPersonName.noDataHandler()
            holder.mTimeTV.text = data.createTime.noDataHandler()
            holder.mNo.text = data.dismantleNo.noDataHandler()
            val str = StringBuilder()
            data.partsCodeList?.forEach {
                str.append("$it,")
            }
            var s = ""
            s = str.toString()
            s = if (s.isNotEmpty()) {
                s.substring(0, str.length - 1)
            } else {
                "暂无数据"
            }
            holder.partsCodeList.text = s


            holder.mPlanTimeTV.text = data.plainFinishTime.noDataHandler()
            holder.mFinishTimeTV.text = data.realFinishTime.noDataHandler()
            holder.mFinishTimeLL.visibility = if (data.status == 2) View.VISIBLE else View.GONE
            holder.mBottomRL.visibility = if (data.status != 2) View.VISIBLE else View.GONE
            holder.mLine.visibility = if (data.status != 2) View.VISIBLE else View.GONE

            val dismantleMethod = when (data.dismantleMethod) {
                1 -> "毁型"
                2 -> "拆配件"
                else -> "暂无数据"
            }
            holder.dismantleMethod.text = dismantleMethod
//
//
            holder.mBottomRL.setOnClickListener {
                holder.mBTN.performClick()
            }
            holder.mBTN.setOnClickListener {
                //拆解
                listener.onHandlerPart(position)

            }
            holder.mParentLL.setOnClickListener {
                //查看详情

                context.startActivity<PartDetailActivity>(MyConstants.ORDER_ID to data.orderId)
//                context.startActivity<BatteryDetailActivity>(MyConstants.ORDER_ID to data.orderId)
            }
            holder.mDeleteBTN.visibility = if (data.isRemove == 1) View.VISIBLE else View.GONE
            holder.mDeleteBTN.setOnClickListener {
                listener.itemDelete(position,data)
            }
        }
    }

    interface PartHandlerAdapterListener {
        fun onHandlerPart(position: Int)
        fun itemDelete(position: Int,data:PartHandlerListEntity.DataBean.ListBean)
    }

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val mUserNameTV: TextView = view.findViewById(R.id.mUserNameTV)
        val partsCodeList: TextView = view.findViewById(R.id.partsCodeList)
        val mNo: TextView = view.findViewById(R.id.mNo)
        val mTimeTV: TextView = view.findViewById(R.id.mTimeTV)
        val dismantleMethod: TextView = view.findViewById(R.id.dismantleMethod)
        val mBottomRL: RelativeLayout = view.findViewById(R.id.mBottomRL)
        val mBTN: Button = view.findViewById(R.id.mBTN)
        val mParentLL: LinearLayoutCompat = view.findViewById(R.id.mParentLL)
        val mPlanTimeTV: TextView = view.findViewById(R.id.mPlanTimeTV)
        val mFinishTimeLL: LinearLayoutCompat = view.findViewById(R.id.mFinishTimeLL)
        val mFinishTimeTV: TextView = view.findViewById(R.id.mFinishTimeTV)
        val mLine: View = view.findViewById(R.id.mLine)
        val mDeleteBTN: Button = view.findViewById(R.id.mDeleteBTN)
    }


}